import flash.events.Event;

import mx.controls.Alert;
import mx.rpc.events.FaultEvent;

       
       //Contains name for the best 25 schools about a general area
 [Bindable]
 public var genFieldListing:Array = [
                {label:"Best Business Schools", data:"mba"},
                {label:"Best Law Schools", data:"law"},
                {label:"Best Medical Schools", data:"med"},
                {label:"Best Engineering Schools", data:"eng"},
                {label:"Best Education Schools", data: "edu"},
                {label:"Best Public Affairs Schools", data: "pad"}                
                ];
       //Contains url for the best 25 schools about a general area         
 								
//generate the General Listing
//takes as input field:= the data arg of the array entry 
function genURLListing(field:String):String{
	return "http://grad-schools.usnews.rankingsandreviews.com/grad/" + field + "/search";	
}
//generate the Specific Listing within the General Field
function specURLListing(field:String, specField):String{
	return "http://grad-schools.usnews.rankingsandreviews.com/grad/" + field + "/" + specField;	
}

function obtainHTML(url:String):void{
	var assetLoader:URLLoader = new URLLoader();
    var request:URLRequest = new URLRequest(url);
	assetLoader.load(request);
    assetLoader.addEventListener(Event.COMPLETE, loadPageComplete);
    assetLoader.addEventListener(FaultEvent.FAULT, failureHandler);
}           

public function getfieldListing():Array{
	return this.genFieldListing;
}
  
function loadPageComplete(e:Event):void{
	var pageText:String = String(e.currentTarget.data);
	var oppattern:RegExp =  /<table>/gx;
	var clpattern:RegExp =  /<\/table>/gx;
	var closedTag:String =  String(clpattern);
	var openIndex:int = pageText.search(oppattern);
	var closeIndex:int = pageText.search(clpattern);
	var tableText:String = pageText.substring(openIndex,closeIndex + closedTag.length); 
	// Get html <table> into xml format
	var htmlXML:XML = XML(tableText);
	// Get schools into xml format by creating a "well formated" subXML structure
	var schoolStr:String = "<schoolList>  \n" + htmlXML.tbody.tr.(attribute("class")=="school-title") 
								+ " \n </schoolList>";
	var schoolXML:XML = new XML(schoolStr);
	//outputField.text += schoolXML;
		
	for each (var gradSchool:XML in schoolXML.tr){
    	var rank:int = Number(gradSchool.td.(attribute("class")=="rank"));
    	var schoolName:String = gradSchool.td.(attribute("class")=="school").a.(attribute("href"));
    	var address:String = gradSchool.td.(attribute("class")=="school").address;
    	outputField.text += "Rank: " + rank + " \n \t Institution: " + schoolName + " \n \t Location: " + address + "\n"; 
	}	
}
      
function failureHandler(e:Event):void{
    outputField.text += "HTML Loading Error";
}
function changeEvent(e:Event){
   	Alert.show(e.currentTarget.selectedItem.label);
    outputField.text+=e.currentTarget.selectedItem.label + " " + 
    					       e.currentTarget.selectedIndex + "\n";
}
function comboChangeEvt(e:Event):void{
    var selectionString:String = e.currentTarget.selectedItem.label;
    var selectionData:String = e.currentTarget.selectedItem.data;
    outputField.text = "";//erase previous work
    outputField.text += "Topic:  " +  selectionString + "\n";
    outputField.text += "-------------------------------" + "\n";
    var selectionIndex:int = e.currentTarget.selectedIndex;
    //this.obtainHTML(genUrlListing[selectionIndex]);
    this.obtainHTML(genURLListing(selectionData));
}
      